package poker;

import java.util.ArrayList;
import java.util.List;

import enums.ActionType;

public class Printer 
{
	private static Logger logger;
	private static boolean enabled;
	
	public static void setEnabled(boolean value) {enabled = value; }
	
	static
	{
		logger = Logger.getInstance();
		enabled = true;
	}
	
	private static void print(String text)
	{
		if(enabled)
		{
			System.out.print(text);
			logger.Log(text);	
		}
	}
	
	public static void printAvailbleActions(Player player, ArrayList<ActionType> actions)
	{
		StringBuilder sb = new StringBuilder();
		sb.append(player + ": Cash: " + player.getCash() + "\n");
		for (int i = 0; i < actions.size(); i++) {
			sb.append(i + ": "+ actions.get(i) + " ");
		}
		sb.append("\n");
		
		print(sb.toString());
	}
	
	public static void printActionFold(Player player)
	{
		String sb = player + ": folded\n\n";

		print(sb);
	}
	
	public static void printActionCheck(Player player)
	{
		String sb = player + " checked\n\n";
		print(sb);
	}
	
	public static void printValueToCall(Player player, int valueToCall)
	{	
		String sb;
		if(Rules.isCheckAllowed(player))
		{ 
			sb = player + ": can check.\n";
		}
		else
		{
			sb = player + ": " + valueToCall + " to call.\n";
		}

		print(sb);
	}
	
	public static void printActionCall(Player player, int sum) 
	{ 
		String sb = player + ": called " + sum + "\n\n"; 
		print(sb);
	}
	
	public static void printActionRaise(Player player, int raiseSum) 
	{ 
		String sb = player + ": raised to " + raiseSum + "\n\n"; 
		print(sb);
	}
	
	public static void printActionBet(Player player, int betSum) 
	{
		String sb = player + ": bet " + betSum + "\n\n"; 
		print(sb);
	}

	public static void printWinner(ArrayList<Player> winners, GameRound gameRound, boolean show) {
		
		StringBuilder sb = new StringBuilder();
		if(winners.size() == 1)
			sb.append("The winner is: ");
		else
			sb.append("The winners are: \n");
		
		if(show)
		{
			int[] handRating = HandPowerRating.rate(gameRound.getHand(winners.get(0)));
		
			for (Player winner : winners) {
				sb.append(winner + " with " + HandPowerRating.nameOfHand(handRating) + "\n");
			}
		}
		else
		{
			for (Player winner : winners) 
			{
				sb.append(winner + "\n");
			}
		}
		sb.append("\n");

		print(sb.toString());
	}
	
	public static void printStartInformation(Player dealer, Player smallBlind, Player bigBlind)
	{
		StringBuilder sb = new StringBuilder("New Round!\n");
		sb.append("Dealer is: "+ dealer.getName() + "\n");
		sb.append("Small blind is: " + smallBlind.getName() + "\n");
		sb.append("Big blind is : " + bigBlind.getName() + "\n\n");
	
		print(sb.toString());
	}

	public static void printPocketCards(List<Player> players) 
	{
		StringBuilder sb = new StringBuilder("Pocket cards:\n");
		
		for (int j = 0; j < players.size(); j++) 
		{
			sb.append(players.get(j) + ": " + players.get(j).getPocketCardsAsString() + "\n");			
		}	
		sb.append("\n");
		print(sb.toString());
	}

	public static void printFlop(Card[] flop) 
	{
		String sb = "Flop is: " + flop[0] + " " + flop[1]+ " " + flop[2] + "\n";
		print(sb);
	}

	public static void printTurn(Card turnCard) 
	{
		String sb = "The turn is " + turnCard + "\n";
		print(sb);
	}

	public static void printRiver(Card riverCard) 
	{
		String sb = "The river is " + riverCard + "\n";
		print(sb);
	}
	
	public static void printPlayersLeft(List<Player> players)
	{
		StringBuilder sb = new StringBuilder("The players left are: ");
		for (Player player : players) {
			if(!player.getFolded())
				sb.append(player + " ");
		}
		sb.append("\n");
		print(sb.toString());
	}
	
	public static void printEachPlayersTotalWins(List<Player> players)
	{
		StringBuilder sb = new StringBuilder("Total winnings are: ");
		sb.append("\n");
		for (int i = 0; i < players.size(); i++){
			sb.append(players.get(i).getName() + " wins: " + players.get(i).getTotalWinsThisGame()
					+ " with a total cash of: " + players.get(i).getCash());
			sb.append("\n");
		}
		print(sb.toString());
	}
}
